package com.jingdianjichi.practice.server.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Preconditions;
import com.jingdianjichi.practice.api.common.PageResult;
import com.jingdianjichi.practice.api.common.Result;
import com.jingdianjichi.practice.api.req.*;
import com.jingdianjichi.practice.api.resp.*;
import com.jingdianjichi.practice.server.entity.dto.PracticeSetDTO;
import com.jingdianjichi.practice.server.entity.dto.PracticeSubjectDTO;
import com.jingdianjichi.practice.server.service.PracticeSetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 练题套卷Controller
 */
@RestController
@RequestMapping("/practice/set")
@Slf4j
public class PracticeSetController {

    @Resource
    private PracticeSetService setService;

    /**
     *  获取专项练习的所有标签
     */
    @RequestMapping("/getSpecialPracticeContent")
    public Result<List<SpecialPracticeVO>> getSpecialPracticeContent(){
        try {
            List<SpecialPracticeVO> resultList=setService.getSpecialPracticeContent();

            if (log.isInfoEnabled()){
                log.info("PracticeController.getSpecialPracticeContent.resultList:{}", JSON.toJSONString(resultList));
            }

            return Result.ok(resultList);
        }catch (Exception e){
            log.error("PracticeController.getSpecialPracticeContent.error:{}",e.getMessage(),e);

            return Result.fail("获取专项练习内容失败亲");
        }
    }


    /**
     * 开始练习,返回就是生成的套卷的秒速
     */
    @PostMapping("/addPractice")
    public Result<PracticeSetVO> addPractice(@RequestBody GetPracticeSubjectListReq req){
        if(log.isInfoEnabled()){
            log.info("练题题目入参:{}",JSON.toJSONString(req));
        }

        try {
            //参数校验
            Preconditions.checkArgument(!Objects.isNull(req),"参数不能是空的");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(req.getAssembleIds()),"标签的ids不能是空的");

            //封装参数
            PracticeSubjectDTO dto = new PracticeSubjectDTO();
            dto.setAssembleIds(req.getAssembleIds());

            //传递
            PracticeSetVO practiceSetVo=setService.addPractice(dto);

            if (log.isInfoEnabled()){
                log.info("获取练习题目列表出参:{}", JSONObject.toJSONString(practiceSetVo));
            }

            //返回数据
            return Result.ok(practiceSetVo);

        }catch (IllegalArgumentException e){
            log.error("参数异常.错误原因:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
        catch (Exception e){
            return Result.fail("获取练习题目异常");
        }
    }


    /**
     * 获得练习的题目,通过套卷的表查询题目,传递题目类型,前端要去做样式的亲
     */
    @PostMapping("/getSubjects")
    public Result<PracticeSubjectListVO> getSubjects(@RequestBody GetPracticeSubjectsReq req){
        if (log.isInfoEnabled()){
            log.info("获得练习题的入参:{}",JSON.toJSONString(req));     //就是传递套卷的id
        }

        try {
            //参数校验
            Preconditions.checkArgument(!Objects.isNull(req),"参数不能是空的");
            Preconditions.checkArgument(!Objects.isNull(req.getSetId()),"练习题的id不能是空的");

            PracticeSubjectListVO vo=setService.getSubjects(req);

            log.info("获得练习题的出参:{}",JSON.toJSONString(vo));

            return Result.ok(vo);

        }catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("获取练习题目列表异常！错误原因{}", e.getMessage(), e);
            return Result.fail("获取练习题目列表异常！");
        }
    }


    /**
     * 获取题目详情,上面就仅仅是获得了题目的id和题目的类型,前端先把样式做出来,这里就是获得题目的详细信息
     */
    @PostMapping(value = "/getPracticeSubject")
    public Result<PracticeSubjectVO> getPracticeSubject(@RequestBody GetPracticeSubjectReq req) {
        if (log.isInfoEnabled()) {
            log.info("获取练习题详情入参{}", JSON.toJSONString(req));
        }
        try {
            //参数校验
            Preconditions.checkArgument(!Objects.isNull(req), "参数不能为空！");
            Preconditions.checkArgument(!Objects.isNull(req.getSubjectId()), "题目id不能为空！");
            Preconditions.checkArgument(!Objects.isNull(req.getSubjectType()), "题目类型不能为空！");

            PracticeSubjectDTO dto = new PracticeSubjectDTO();
            dto.setSubjectId(req.getSubjectId());
            dto.setSubjectType(req.getSubjectType());

            PracticeSubjectVO vo = setService.getPracticeSubject(dto);

            if (log.isInfoEnabled()) {
                log.info("获取练习题目详情出参{}", JSON.toJSONString(vo));
            }

            return Result.ok(vo);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("获取练习详情异常！错误原因{}", e.getMessage(), e);
            return Result.fail("获取练习题目详情异常！");
        }
    }


    /**
     * 获取模拟套题内容,我们预先定义的不是用户现场生成的亲
     */
    @PostMapping(value = "/getPreSetContent")
    public Result<PageResult<PracticeSetVO>> getPreSetContent(@RequestBody GetPreSetReq req) {
        if (log.isInfoEnabled()) {
            log.info("获取模拟套题内容入参{}", JSON.toJSONString(req));
        }
        try {
            //参数校验
            Preconditions.checkArgument(!Objects.isNull(req), "参数不能为空！");

            PracticeSetDTO dto = new PracticeSetDTO();
            dto.setOrderType(req.getOrderType());               //排序规则
            dto.setPageInfo(req.getPageInfo());                 //分页参数
            dto.setSetName(req.getSetName());                   //套题名称

            //获得套题列表
            PageResult<PracticeSetVO> list = setService.getPreSetContent(dto);

            if (log.isInfoEnabled()) {
                log.info("获取模拟套题内容出参{}", JSON.toJSONString(list));
            }

            return Result.ok(list);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("获取模拟套题内容异常！错误原因{}", e.getMessage(), e);
            return Result.fail("获取模拟套题内容异常！");
        }
    }

    /**
     * 获取未完成的练题内容,这些都是用户中途退出来的,删除字段还是0,放弃了才会变成1的
     */
    @PostMapping(value = "/getUnCompletePractice")
    public Result<PageResult<UnCompletePracticeSetVO>> getUnCompletePractice(@RequestBody GetUnCompletePracticeReq req) {
        try {
            Preconditions.checkArgument(!Objects.isNull(req), "参数不能为空！");

            //获取未完成的套题
            PageResult<UnCompletePracticeSetVO> list = setService.getUnCompletePractice(req);

            if (log.isInfoEnabled()) {
                log.info("获取未完成练习内容出参{}", JSON.toJSONString(list));
            }

            return Result.ok(list);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("获取未完成练习内容异常！错误原因{}", e.getMessage(), e);
            return Result.fail("获取未完成练习内容异常！");
        }
    }
}
